package com.example.user.controller;

/**
 * @author FanFu
 * @version 1.0
 * @date 2025/11/8 20:18
 */


import com.example.common.model.Result;
import com.example.user.entity.User;
import com.example.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public Result<User> getUserById(@PathVariable @NotNull Long id) {
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    @GetMapping("/username/{username}")
    public Result<User> getUserByUsername(@PathVariable String username) {
        User user = userService.getUserByUsername(username);
        return Result.success(user);
    }

    @PostMapping
    public Result<Boolean> addUser(@RequestBody @Valid User user) {
        boolean result = userService.save(user);
        return Result.success(result);
    }

    @PutMapping
    public Result<Boolean> updateUser(@RequestBody @Valid User user) {
        boolean result = userService.updateById(user);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteUser(@PathVariable @NotNull Long id) {
        boolean result = userService.removeById(id);
        return Result.success(result);
    }

    @GetMapping("/list")
    public Result<List<User>> getUserList() {
        List<User> list = userService.list();
        return Result.success(list);
    }
}